package com.example.comic.adpter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.comic.R
import com.example.comic.mvp.model.Chapter
import kotlinx.android.synthetic.main.item_chapter.view.*

class ChapterAdapter(var data: List<Chapter> = ArrayList(), val itemClick: (View, Int) -> Unit) :
        RecyclerView.Adapter<ChapterAdapter.PageViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChapterAdapter.PageViewHolder {
        return PageViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_chapter, parent, false))
    }

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: PageViewHolder, position: Int) {
        bindView(holder.itemView, position)
    }

    private fun bindView(itemView: View, position: Int) {
        var chapter = data[position]
        itemView.tvTitle.text = chapter.title
        itemView.tvTitle.setOnClickListener {
            itemClick(itemView, position)
        }
    }

    fun refreshData(newData: List<Chapter>) {
        data = newData
        notifyDataSetChanged()
    }

    class PageViewHolder(iteView: View) : RecyclerView.ViewHolder(iteView)
}